
*=============================================================================

*** Setup

version 15
capture log close
set more off
clear all

* Load data
use "$data/de_prep_import.dta"

*=============================================================================

*** Prepare data

* Keep IDs and ctb choices
keep caseid CTB_*

* Rename the 24 question variables c1 to c24.
* These record which choice respondents made in each of the 24 questions (1 to 6)
gen c1=CTB_4_W5_Q1
gen c2=CTB_4_W5_Q2
gen c3=CTB_4_W5_Q3 
gen c4=CTB_4_W5_Q4 
gen c5=CTB_4_W5_Q5 
gen c6=CTB_4_W5_Q6 
gen c7=CTB_4_W9_Q1
gen c8=CTB_4_W9_Q2
gen c9=CTB_4_W9_Q3
gen c10=CTB_4_W9_Q4
gen c11=CTB_4_W9_Q5
gen c12=CTB_4_W9_Q6
gen c13=CTB_4_W10_Q1
gen c14=CTB_4_W10_Q2
gen c15=CTB_4_W10_Q3
gen c16=CTB_4_W10_Q4
gen c17=CTB_4_W10_Q5
gen c18=CTB_4_W10_Q6
gen c19=CTB_4_W14_Q1
gen c20=CTB_4_W14_Q2
gen c21=CTB_4_W14_Q3
gen c22=CTB_4_W14_Q4
gen c23=CTB_4_W14_Q5
gen c24=CTB_4_W14_Q6

keep caseid c1-c24

* Reshape to data structure with 24 observations for each respondent, 
* such that each row is a unique respondent-choice observations 
* (the survey has 24 ctb choice questions)
reshape long c, i(caseid) j(budget_number)

*=============================================================================

*** Get payoff information

* Instrument details from Andreoni et al. 2015
sort budget_number
merge m:1 budget_number using "$data/01 Raw Data/ctbinput"
drop _merge

* Choice variables
gen t0=.
replace t0=1 if sooner_date==0
replace t0=0 if sooner_date!=0 & sooner_date!= .

gen k=.
replace k=delay_weeks*7

* Sooner and later endowments to determine values for each option
* and compute the price ratio
order endowment_soon endowment_later
gen pratio=endowment_later/endowment_soon
tab pratio

* Define values for each option
forvalues i=1(1)5 {
gen soon_`i'=20/pratio-(`i'-1)*(20/pratio)/(5)
}
gen soon_6=0

forvalues i = 1(1)6 {
gen late_`i'=(`i'-1)*4
}

* Associate choice options with values
gen sooner_choice=.
forvalues i=1(1)6 {
replace sooner_choice = soon_`i' if c==`i'
}

* Within-respondent variation
egen choice_sd=sd(c), by(caseid)

* Indicator for always sooner/later
gen cbt_alw_soonest=0
replace cbt_alw_soonest=1 if choice_sd==0 & c==1
gen cbt_alw_latest=0
replace cbt_alw_latest=1 if choice_sd==0 & c==6

* Chosen sooner and later payouts for estimation
g payout_soon=.
forval i=1(1)6 {
replace payout_soon=soon_`i' if c==`i'
}
g payout_late=.
forval i=1(1)6 {
replace payout_late=late_`i' if c==`i'
}

* Follow Andreoni et al.'s replication code
* and replace 0 with .001 (although JEBO article says
* they replaced with .01)
g payout_soon_adjusted=payout_soon
replace payout_soon_adjusted=0.001 if payout_soon==0
g payout_late_adjusted=payout_late
replace payout_late_adjusted=0.001 if payout_late==0

g allocation_ratio_adjusted=(payout_soon_adjusted/payout_late_adjusted)
g log_allocation_ratio=ln(allocation_ratio_adjusted)

* This is the dependent variable ln(x_star/y_star) in the Andreoni code:
gen ln_consrat_star=log_allocation_ratio

* Log price ratio
g ln_pratio=ln(pratio)


*=============================================================================

*** Individual-level CTB estimation

* Prepare vars for estimates and SEs
gen a_ind_ctb = .
gen b_ind_ctb = .
gen d_ind_ctb = .

label var a_ind_ctb "Estimate: Risk Aversion: Alpha"
label var b_ind_ctb "Estimate: Present Bias: Beta"
label var d_ind_ctb "Estimate: Long-run Discounting: Delta"

gen a_ind_ctb_se = .
gen b_ind_ctb_se = .
gen d_ind_ctb_se = .

label var a_ind_ctb_se "Standard Error: Alpha"
label var b_ind_ctb_se "Standard Error: Beta"
label var d_ind_ctb_se "Standard Error: Delta"

* Generate consecutive ID to loop over in individual-level estimations
egen id=group(caseid)
order id
* Save maximum for loop
qui su id, det

* Individual-level estimation: 
* Regress the logged allocation ratio for the chosen option on a
* present period indicator t0
* the delay k in days
* the logged price ratio of the options in the budget
forvalues i = 1(1)`r(max)' {
	di "Individual `i'"
	cap reg ln_consrat_star t0 k ln_pratio if id==`i' & choice_sd!=0 , noconstant 
	cap nlcom (a: 1/_b[ln_pratio] + 1) (b: exp(_b[t0]/_b[ln_pratio])) (d: exp(_b[k]/_b[ln_pratio])), post
	cap replace a_ind_ctb = _b[a] if id == `i' 
	cap replace b_ind_ctb = _b[b] if id == `i' 
	cap replace d_ind_ctb = _b[d] if id == `i' 
	cap replace a_ind_ctb_se = _se[a] if id == `i' 
	cap replace b_ind_ctb_se = _se[b] if id == `i'
	cap replace d_ind_ctb_se = _se[d] if id == `i' 
}

* Set to missing if no variation
replace a_ind_ctb=. if choice_sd==0
replace b_ind_ctb=. if choice_sd==0
replace d_ind_ctb=. if choice_sd==0
replace a_ind_ctb_se=. if choice_sd==0
replace b_ind_ctb_se=. if choice_sd==0
replace d_ind_ctb_se=. if choice_sd==0

gen r_ind_ctb = (1/d_ind_ctb)^365 - 1
label var r_ind_ctb "Estimate: Annualized Discount Rate"

*=============================================================================

*** Create risk acceptance variable

* Keep one obs per respondent and save to merge with other survey variables
gen patience_ctb=d_ind_ctb
lab var patience_ctb "Patience CTB"

gen riskacceptance_ctb=a_ind_ctb
lab var riskacceptance_ctb "Risk Acceptance CTB"

bysort id: gen aux=_n
keep if aux==1

* Generate thresholded patience and risk indicators using the median
gen patience_ctb_high=.
su d_ind_ctb, det
replace patience_ctb_high=0 if d_ind_ctb<=`r(p50)'
replace patience_ctb_high=1 if d_ind_ctb>`r(p50)' & d_ind_ctb!=.
label var patience_ctb_high "Patience High (delta>median)"

gen riskacceptance_ctb_high=.
su a_ind_ctb, det
replace riskacceptance_ctb_high=0 if a_ind_ctb<=`r(p50)'
replace riskacceptance_ctb_high=1 if a_ind_ctb>`r(p50)' & a_ind_ctb!=.
label var riskacceptance_ctb_high "Risk Acceptance High (alpha>median)"

* Winsorize at 5th and 95th percentile 
gen riskacceptance_ctb_win=.
gen riskacceptance_ctb_outlier=0
gen riskacceptance_ctb_outlierp5=0
gen riskacceptance_ctb_outlierp95=0

su riskacceptance_ctb, det
replace riskacceptance_ctb_win=riskacceptance_ctb
replace riskacceptance_ctb_win=`r(p95)' if riskacceptance_ctb>=`r(p95)' & riskacceptance_ctb!=.
replace riskacceptance_ctb_win=`r(p5)' if riskacceptance_ctb<=`r(p5)'  & riskacceptance_ctb!=. 
replace riskacceptance_ctb_outlier=1 if (riskacceptance_ctb>=`r(p95)' | riskacceptance_ctb<=`r(p5)') & riskacceptance_ctb!=.
replace riskacceptance_ctb_outlierp5=1 if riskacceptance_ctb<=`r(p5)' & riskacceptance_ctb!=.
replace riskacceptance_ctb_outlierp95=1 if riskacceptance_ctb>=`r(p95)' & riskacceptance_ctb!=.

lab var riskacceptance_ctb_win "Risk Acceptance CTB winsorized" 
lab var riskacceptance_ctb_outlier "Risk Acceptance CTB 10% outlier"
lab var riskacceptance_ctb_outlierp5 "Risk Acceptance CTB 5% p5 outlier"
lab var riskacceptance_ctb_outlierp95 "Risk Acceptance CTB 5% p95 outlier"

* Trim at 5th and 95th percentile 
gen riskacceptance_ctb_trim=.
su riskacceptance_ctb, det
replace riskacceptance_ctb_trim=riskacceptance_ctb
replace riskacceptance_ctb_trim=. if riskacceptance_ctb>`r(p95)'
replace riskacceptance_ctb_trim=. if riskacceptance_ctb<`r(p5)' 
lab var riskacceptance_ctb_trim "Risk Acceptance CTB trimmed"

* Generate binary indicators
su riskacceptance_ctb_trim, det
gen riskacceptance_ctb_trim_high=.
replace riskacceptance_ctb_trim_high=0 if riskacceptance_ctb_trim<r(p50)
replace riskacceptance_ctb_trim_high=1 if riskacceptance_ctb_trim>=r(p50) & riskacceptance_ctb_trim!=.

su riskacceptance_ctb_win, det
gen riskacceptance_ctb_win_high=.
replace riskacceptance_ctb_win_high=0 if riskacceptance_ctb_win<r(p50)
replace riskacceptance_ctb_win_high=1 if riskacceptance_ctb_win>=r(p50) & riskacceptance_ctb_win!=.

*=============================================================================

*** Create patience variable

* Winsorize at 5th and 95th percentile - within treatment group and for the pooled sample
g patience_ctb_win=.
g patience_ctb_outlier=0
g patience_ctb_outlierp5=0
g patience_ctb_outlierp95=0

su patience_ctb, det
replace patience_ctb_win=patience_ctb if choice_sd!=0
replace patience_ctb_win=`r(p95)' if patience_ctb>`r(p95)' & patience_ctb!=. & choice_sd!=0
replace patience_ctb_win=`r(p5)' if patience_ctb<`r(p5)' & patience_ctb!=. & choice_sd!=0
replace patience_ctb_outlier=1 if (patience_ctb>`r(p95)' | patience_ctb<=`r(p5)') & patience_ctb!=. & choice_sd!=0
replace patience_ctb_outlierp5=1 if patience_ctb<`r(p5)' & patience_ctb!=. & choice_sd!=0
replace patience_ctb_outlierp95=1 if patience_ctb>`r(p95)' & patience_ctb!=. & choice_sd!=0

lab var patience_ctb_win "Patience CTB winsorized" 
lab var patience_ctb_outlier "Patience CTB 10 % outlier"
lab var patience_ctb_outlierp5 "Patience CTB 5 % p5 outlier"
lab var patience_ctb_outlierp95 "Patience CTB 5 % p95 outlier"

su patience_ctb, det
g patience_ctb_win_pooled=patience_ctb
g patience_ctb_outlier_p=0
g patience_ctb_outlierp5_p=0
g patience_ctb_outlierp95_p=0
replace patience_ctb_win_pooled=`r(p95)' if patience_ctb>`r(p95)' & patience_ctb!=. & choice_sd!=0
replace patience_ctb_win_pooled=`r(p5)' if patience_ctb<`r(p5)' & patience_ctb!=.
replace patience_ctb_outlier_p=1 if (patience_ctb>`r(p95)' | patience_ctb<`r(p5)') & patience_ctb!=. & choice_sd!=0
replace patience_ctb_outlierp5_p=1 if patience_ctb<`r(p5)' & patience_ctb!=.
replace patience_ctb_outlierp95_p=1 if patience_ctb>`r(p95)' & patience_ctb!=.
lab var patience_ctb_win_pooled "Patience CTB winsorized pooled"
lab var patience_ctb_outlier_p "Patience CTB 10 % outlier pooled"
lab var patience_ctb_outlierp5_p "Patience CTB 5 % p5 outlier pooled"
lab var patience_ctb_outlierp95_p "Patience CTB 5 % p95 outlier pooled"

* Trim at 5th and 95th percentile
su patience_ctb, det
g patience_ctb_trim=.
replace patience_ctb_trim=patience_ctb
replace patience_ctb_trim=. if patience_ctb>`r(p95)' 
replace patience_ctb_trim=. if patience_ctb<`r(p5)'  
lab var patience_ctb_trim "Patience CTB trimmed"

* Generate binary indicators
su patience_ctb_trim, det
gen patience_ctb_trim_high=.
replace patience_ctb_trim_high=0 if patience_ctb_trim<`r(p50)'
replace patience_ctb_trim_high=1 if patience_ctb_trim>=`r(p50)' & patience_ctb_trim!=.

su patience_ctb_win, det
gen patience_ctb_win_high=.
replace patience_ctb_win_high=0 if patience_ctb_win<`r(p50)'
replace patience_ctb_win_high=1 if patience_ctb_win>=`r(p50)' & patience_ctb_win!=.

*=============================================================================

*** Saving
save "$data/de_ctb_formerge.dta", replace
